package com.djj.nanny.controller;

import jakarta.annotation.Resource;
import com.djj.nanny.common.Result;
import com.djj.nanny.entity.ChatMessage;
import com.djj.nanny.service.ChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.djj.nanny.dto.SendMessageDTO;

@RestController
@RequestMapping("/chat")
public class ChatController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChatController.class);
    
    @Resource
    private ChatService chatService;

    /**
     * 创建聊天会话
     */
    @PostMapping("/sessions")
    public Result<?> createSession(
            @RequestParam Long userId,
            @RequestParam Long staffId) {
        LOGGER.info("创建聊天会话: userId={}, staffId={}", userId, staffId);
        return Result.success(chatService.createSession(userId, staffId));
    }

    /**
     * 获取聊天会话列表
     */
    @GetMapping("/sessions")
    public Result<?> getChatSessions(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        LOGGER.info("获取聊天会话列表: userId={}, pageNum={}, pageSize={}", userId, pageNum, pageSize);
        return Result.success(chatService.getChatSessions(userId, pageNum, pageSize));
    }

    /**
     * 获取聊天记录
     */
    @GetMapping("/messages")
    public Result<?> getChatMessages(
            @RequestParam Long sessionId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        LOGGER.info("获取聊天记录: sessionId={}, pageNum={}, pageSize={}", sessionId, pageNum, pageSize);
        return Result.success(chatService.getChatMessages(sessionId, pageNum, pageSize));
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/messages/read")
    public Result<?> markMessagesAsRead(
            @RequestParam Long sessionId,
            @RequestParam Long userId) {
        LOGGER.info("标记消息为已读: sessionId={}, userId={}", sessionId, userId);
        chatService.markMessagesAsRead(sessionId, userId);
        return Result.success();
    }
    
    /**
     * 发送消息
     */
    @PostMapping("/messages/send")
    public Result<?> sendMessage(@RequestBody SendMessageDTO dto) {
        LOGGER.info("发送消息: senderId={}, receiverId={}", dto.getSenderId(), dto.getReceiverId());
        try {
            ChatMessage message = chatService.sendMessage(dto.getSenderId(), dto.getReceiverId(), dto.getContent());
            return Result.success(message);
        } catch (Exception e) {
            LOGGER.error("发送消息失败", e);
            return Result.error("发送消息失败: " + e.getMessage());
        }
    }

    /**
     * 删除单条聊天消息
     */
    @DeleteMapping("/messages/{messageId}")
    public Result<?> deleteMessage(@PathVariable Long messageId) {
        LOGGER.info("删除聊天消息: messageId={}", messageId);
        chatService.deleteMessage(messageId);
        return Result.success();
    }

    /**
     * 清空会话的所有聊天记录
     */
    @DeleteMapping("/sessions/{sessionId}/messages")
    public Result<?> clearSessionMessages(@PathVariable Long sessionId) {
        LOGGER.info("清空会话聊天记录: sessionId={}", sessionId);
        chatService.clearSessionMessages(sessionId);
        return Result.success();
    }

    /**
     * 删除聊天会话
     */
    @DeleteMapping("/sessions/{sessionId}")
    public Result<?> deleteSession(@PathVariable Long sessionId) {
        LOGGER.info("删除聊天会话: sessionId={}", sessionId);
        chatService.deleteSession(sessionId);
        return Result.success();
    }

    /**
     * 获取用户未读消息总数
     */
    @GetMapping("/unread/count")
    public Result<?> getUnreadMessageCount(@RequestParam Long userId) {
        LOGGER.info("获取未读消息总数: userId={}", userId);
        return Result.success(chatService.getUnreadMessageCount(userId));
    }

    /**
     * 获取所有聊天记录(管理后台)
     */
    @GetMapping("/messages/all")
    public Result<?> getAllChatMessages(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String content) {
        LOGGER.info("获取所有聊天记录: pageNum={}, pageSize={}, content={}", pageNum, pageSize, content);
        return Result.success(chatService.getAllChatMessages(pageNum, pageSize, content));
    }
} 